{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Money</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1990-12-19</th>\n",
       "      <td></td>\n",
       "      <td>96.0500</td>\n",
       "      <td>99.9800</td>\n",
       "      <td>95.7900</td>\n",
       "      <td>99.9800</td>\n",
       "      <td>126000</td>\n",
       "      <td>4.940000e+05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-20</th>\n",
       "      <td>99.98</td>\n",
       "      <td>104.3000</td>\n",
       "      <td>104.3900</td>\n",
       "      <td>99.9800</td>\n",
       "      <td>104.3900</td>\n",
       "      <td>19700</td>\n",
       "      <td>8.400000e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-21</th>\n",
       "      <td>104.39</td>\n",
       "      <td>109.0700</td>\n",
       "      <td>109.1300</td>\n",
       "      <td>103.7300</td>\n",
       "      <td>109.1300</td>\n",
       "      <td>2800</td>\n",
       "      <td>1.600000e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-24</th>\n",
       "      <td>109.13</td>\n",
       "      <td>113.5700</td>\n",
       "      <td>114.5500</td>\n",
       "      <td>109.1300</td>\n",
       "      <td>114.5500</td>\n",
       "      <td>3200</td>\n",
       "      <td>3.100000e+04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-25</th>\n",
       "      <td>114.55</td>\n",
       "      <td>120.0900</td>\n",
       "      <td>120.2500</td>\n",
       "      <td>114.5500</td>\n",
       "      <td>120.2500</td>\n",
       "      <td>1500</td>\n",
       "      <td>6.000000e+03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-25</th>\n",
       "      <td>2901.9518</td>\n",
       "      <td>2891.8918</td>\n",
       "      <td>2897.7674</td>\n",
       "      <td>2872.8497</td>\n",
       "      <td>2886.7416</td>\n",
       "      <td>27463950000</td>\n",
       "      <td>2.732820e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-26</th>\n",
       "      <td>2886.7416</td>\n",
       "      <td>2885.9953</td>\n",
       "      <td>2899.1162</td>\n",
       "      <td>2875.3959</td>\n",
       "      <td>2890.8973</td>\n",
       "      <td>27838753600</td>\n",
       "      <td>2.754430e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-29</th>\n",
       "      <td>2890.8973</td>\n",
       "      <td>2889.4726</td>\n",
       "      <td>2898.9512</td>\n",
       "      <td>2878.5825</td>\n",
       "      <td>2891.8453</td>\n",
       "      <td>25689972700</td>\n",
       "      <td>2.600950e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-30</th>\n",
       "      <td>2891.8453</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2865.1493</td>\n",
       "      <td>2879.2996</td>\n",
       "      <td>26247883700</td>\n",
       "      <td>2.694770e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2879.2996</td>\n",
       "      <td>2877.5409</td>\n",
       "      <td>2940.5927</td>\n",
       "      <td>2876.3009</td>\n",
       "      <td>2938.7493</td>\n",
       "      <td>41272341700</td>\n",
       "      <td>4.188720e+11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8210 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Preclose       Open    Highest     Lowest      Close  \\\n",
       "Day                                                                 \n",
       "1990-12-19               96.0500    99.9800    95.7900    99.9800   \n",
       "1990-12-20      99.98   104.3000   104.3900    99.9800   104.3900   \n",
       "1990-12-21     104.39   109.0700   109.1300   103.7300   109.1300   \n",
       "1990-12-24     109.13   113.5700   114.5500   109.1300   114.5500   \n",
       "1990-12-25     114.55   120.0900   120.2500   114.5500   120.2500   \n",
       "...               ...        ...        ...        ...        ...   \n",
       "2024-07-25  2901.9518  2891.8918  2897.7674  2872.8497  2886.7416   \n",
       "2024-07-26  2886.7416  2885.9953  2899.1162  2875.3959  2890.8973   \n",
       "2024-07-29  2890.8973  2889.4726  2898.9512  2878.5825  2891.8453   \n",
       "2024-07-30  2891.8453  2885.2152  2885.2152  2865.1493  2879.2996   \n",
       "2024-07-31  2879.2996  2877.5409  2940.5927  2876.3009  2938.7493   \n",
       "\n",
       "                 Volume         Money  \n",
       "Day                                    \n",
       "1990-12-19       126000  4.940000e+05  \n",
       "1990-12-20        19700  8.400000e+04  \n",
       "1990-12-21         2800  1.600000e+04  \n",
       "1990-12-24         3200  3.100000e+04  \n",
       "1990-12-25         1500  6.000000e+03  \n",
       "...                 ...           ...  \n",
       "2024-07-25  27463950000  2.732820e+11  \n",
       "2024-07-26  27838753600  2.754430e+11  \n",
       "2024-07-29  25689972700  2.600950e+11  \n",
       "2024-07-30  26247883700  2.694770e+11  \n",
       "2024-07-31  41272341700  4.188720e+11  \n",
       "\n",
       "[8210 rows x 7 columns]"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data=pd.read_csv('000001.csv')\n",
    "data['Day']=pd.to_datetime(data['Day'],format='%Y/%m/%d')\n",
    "data.set_index('Day',inplace=True)\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 计算收益率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Money</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-03</th>\n",
       "      <td>647.8700</td>\n",
       "      <td>637.7200</td>\n",
       "      <td>647.7100</td>\n",
       "      <td>630.5300</td>\n",
       "      <td>639.8800</td>\n",
       "      <td>23451800</td>\n",
       "      <td>1.806930e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-04</th>\n",
       "      <td>639.8800</td>\n",
       "      <td>641.9000</td>\n",
       "      <td>655.5100</td>\n",
       "      <td>638.8600</td>\n",
       "      <td>653.8100</td>\n",
       "      <td>42222000</td>\n",
       "      <td>3.069230e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-05</th>\n",
       "      <td>653.8100</td>\n",
       "      <td>655.3800</td>\n",
       "      <td>657.5200</td>\n",
       "      <td>645.8100</td>\n",
       "      <td>646.8900</td>\n",
       "      <td>43012300</td>\n",
       "      <td>3.015330e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-06</th>\n",
       "      <td>646.8900</td>\n",
       "      <td>642.7500</td>\n",
       "      <td>643.8900</td>\n",
       "      <td>636.3300</td>\n",
       "      <td>640.7600</td>\n",
       "      <td>48748200</td>\n",
       "      <td>3.537580e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-09</th>\n",
       "      <td>640.7600</td>\n",
       "      <td>637.5200</td>\n",
       "      <td>637.5500</td>\n",
       "      <td>625.0400</td>\n",
       "      <td>626.0000</td>\n",
       "      <td>50985100</td>\n",
       "      <td>3.985190e+08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-25</th>\n",
       "      <td>2901.9518</td>\n",
       "      <td>2891.8918</td>\n",
       "      <td>2897.7674</td>\n",
       "      <td>2872.8497</td>\n",
       "      <td>2886.7416</td>\n",
       "      <td>27463950000</td>\n",
       "      <td>2.732820e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-26</th>\n",
       "      <td>2886.7416</td>\n",
       "      <td>2885.9953</td>\n",
       "      <td>2899.1162</td>\n",
       "      <td>2875.3959</td>\n",
       "      <td>2890.8973</td>\n",
       "      <td>27838753600</td>\n",
       "      <td>2.754430e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-29</th>\n",
       "      <td>2890.8973</td>\n",
       "      <td>2889.4726</td>\n",
       "      <td>2898.9512</td>\n",
       "      <td>2878.5825</td>\n",
       "      <td>2891.8453</td>\n",
       "      <td>25689972700</td>\n",
       "      <td>2.600950e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-30</th>\n",
       "      <td>2891.8453</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2865.1493</td>\n",
       "      <td>2879.2996</td>\n",
       "      <td>26247883700</td>\n",
       "      <td>2.694770e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2879.2996</td>\n",
       "      <td>2877.5409</td>\n",
       "      <td>2940.5927</td>\n",
       "      <td>2876.3009</td>\n",
       "      <td>2938.7493</td>\n",
       "      <td>41272341700</td>\n",
       "      <td>4.188720e+11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7182 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Preclose       Open    Highest     Lowest      Close  \\\n",
       "Day                                                                 \n",
       "1995-01-03   647.8700   637.7200   647.7100   630.5300   639.8800   \n",
       "1995-01-04   639.8800   641.9000   655.5100   638.8600   653.8100   \n",
       "1995-01-05   653.8100   655.3800   657.5200   645.8100   646.8900   \n",
       "1995-01-06   646.8900   642.7500   643.8900   636.3300   640.7600   \n",
       "1995-01-09   640.7600   637.5200   637.5500   625.0400   626.0000   \n",
       "...               ...        ...        ...        ...        ...   \n",
       "2024-07-25  2901.9518  2891.8918  2897.7674  2872.8497  2886.7416   \n",
       "2024-07-26  2886.7416  2885.9953  2899.1162  2875.3959  2890.8973   \n",
       "2024-07-29  2890.8973  2889.4726  2898.9512  2878.5825  2891.8453   \n",
       "2024-07-30  2891.8453  2885.2152  2885.2152  2865.1493  2879.2996   \n",
       "2024-07-31  2879.2996  2877.5409  2940.5927  2876.3009  2938.7493   \n",
       "\n",
       "                 Volume         Money  \n",
       "Day                                    \n",
       "1995-01-03     23451800  1.806930e+08  \n",
       "1995-01-04     42222000  3.069230e+08  \n",
       "1995-01-05     43012300  3.015330e+08  \n",
       "1995-01-06     48748200  3.537580e+08  \n",
       "1995-01-09     50985100  3.985190e+08  \n",
       "...                 ...           ...  \n",
       "2024-07-25  27463950000  2.732820e+11  \n",
       "2024-07-26  27838753600  2.754430e+11  \n",
       "2024-07-29  25689972700  2.600950e+11  \n",
       "2024-07-30  26247883700  2.694770e+11  \n",
       "2024-07-31  41272341700  4.188720e+11  \n",
       "\n",
       "[7182 rows x 7 columns]"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new=data['1995':'2024-07'].copy()\n",
    "data_new['Close']= pd.to_numeric(data_new['Close'])\n",
    "data_new['Preclose']= pd.to_numeric(data_new['Preclose'])\n",
    "data_new\n",
    "#copy的意思是深度复制，如果不写copy，Python会假装起个datanew但实际不存在，用copy生成一个新数据\n",
    "#计算机觉得Close和Preclose是字符而不是数字d\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "向量化的处理数据方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Money</th>\n",
       "      <th>Return</th>\n",
       "      <th>Return_2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-03</th>\n",
       "      <td>647.8700</td>\n",
       "      <td>637.7200</td>\n",
       "      <td>647.7100</td>\n",
       "      <td>630.5300</td>\n",
       "      <td>639.8800</td>\n",
       "      <td>23451800</td>\n",
       "      <td>1.806930e+08</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-04</th>\n",
       "      <td>639.8800</td>\n",
       "      <td>641.9000</td>\n",
       "      <td>655.5100</td>\n",
       "      <td>638.8600</td>\n",
       "      <td>653.8100</td>\n",
       "      <td>42222000</td>\n",
       "      <td>3.069230e+08</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-05</th>\n",
       "      <td>653.8100</td>\n",
       "      <td>655.3800</td>\n",
       "      <td>657.5200</td>\n",
       "      <td>645.8100</td>\n",
       "      <td>646.8900</td>\n",
       "      <td>43012300</td>\n",
       "      <td>3.015330e+08</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-06</th>\n",
       "      <td>646.8900</td>\n",
       "      <td>642.7500</td>\n",
       "      <td>643.8900</td>\n",
       "      <td>636.3300</td>\n",
       "      <td>640.7600</td>\n",
       "      <td>48748200</td>\n",
       "      <td>3.537580e+08</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-09</th>\n",
       "      <td>640.7600</td>\n",
       "      <td>637.5200</td>\n",
       "      <td>637.5500</td>\n",
       "      <td>625.0400</td>\n",
       "      <td>626.0000</td>\n",
       "      <td>50985100</td>\n",
       "      <td>3.985190e+08</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-25</th>\n",
       "      <td>2901.9518</td>\n",
       "      <td>2891.8918</td>\n",
       "      <td>2897.7674</td>\n",
       "      <td>2872.8497</td>\n",
       "      <td>2886.7416</td>\n",
       "      <td>27463950000</td>\n",
       "      <td>2.732820e+11</td>\n",
       "      <td>-0.005241</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-26</th>\n",
       "      <td>2886.7416</td>\n",
       "      <td>2885.9953</td>\n",
       "      <td>2899.1162</td>\n",
       "      <td>2875.3959</td>\n",
       "      <td>2890.8973</td>\n",
       "      <td>27838753600</td>\n",
       "      <td>2.754430e+11</td>\n",
       "      <td>0.001440</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-29</th>\n",
       "      <td>2890.8973</td>\n",
       "      <td>2889.4726</td>\n",
       "      <td>2898.9512</td>\n",
       "      <td>2878.5825</td>\n",
       "      <td>2891.8453</td>\n",
       "      <td>25689972700</td>\n",
       "      <td>2.600950e+11</td>\n",
       "      <td>0.000328</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-30</th>\n",
       "      <td>2891.8453</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2865.1493</td>\n",
       "      <td>2879.2996</td>\n",
       "      <td>26247883700</td>\n",
       "      <td>2.694770e+11</td>\n",
       "      <td>-0.004338</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2879.2996</td>\n",
       "      <td>2877.5409</td>\n",
       "      <td>2940.5927</td>\n",
       "      <td>2876.3009</td>\n",
       "      <td>2938.7493</td>\n",
       "      <td>41272341700</td>\n",
       "      <td>4.188720e+11</td>\n",
       "      <td>0.020647</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7182 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Preclose       Open    Highest     Lowest      Close  \\\n",
       "Day                                                                 \n",
       "1995-01-03   647.8700   637.7200   647.7100   630.5300   639.8800   \n",
       "1995-01-04   639.8800   641.9000   655.5100   638.8600   653.8100   \n",
       "1995-01-05   653.8100   655.3800   657.5200   645.8100   646.8900   \n",
       "1995-01-06   646.8900   642.7500   643.8900   636.3300   640.7600   \n",
       "1995-01-09   640.7600   637.5200   637.5500   625.0400   626.0000   \n",
       "...               ...        ...        ...        ...        ...   \n",
       "2024-07-25  2901.9518  2891.8918  2897.7674  2872.8497  2886.7416   \n",
       "2024-07-26  2886.7416  2885.9953  2899.1162  2875.3959  2890.8973   \n",
       "2024-07-29  2890.8973  2889.4726  2898.9512  2878.5825  2891.8453   \n",
       "2024-07-30  2891.8453  2885.2152  2885.2152  2865.1493  2879.2996   \n",
       "2024-07-31  2879.2996  2877.5409  2940.5927  2876.3009  2938.7493   \n",
       "\n",
       "                 Volume         Money    Return  Return_2  \n",
       "Day                                                        \n",
       "1995-01-03     23451800  1.806930e+08 -0.012333         0  \n",
       "1995-01-04     42222000  3.069230e+08  0.021770         0  \n",
       "1995-01-05     43012300  3.015330e+08 -0.010584         0  \n",
       "1995-01-06     48748200  3.537580e+08 -0.009476         0  \n",
       "1995-01-09     50985100  3.985190e+08 -0.023035         0  \n",
       "...                 ...           ...       ...       ...  \n",
       "2024-07-25  27463950000  2.732820e+11 -0.005241         0  \n",
       "2024-07-26  27838753600  2.754430e+11  0.001440         0  \n",
       "2024-07-29  25689972700  2.600950e+11  0.000328         0  \n",
       "2024-07-30  26247883700  2.694770e+11 -0.004338         0  \n",
       "2024-07-31  41272341700  4.188720e+11  0.020647         0  \n",
       "\n",
       "[7182 rows x 9 columns]"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new['Return']=(data_new['Close']/data_new['Preclose'])-1\n",
    "data_new['Return_2']=0\n",
    "data_new\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\18762\\AppData\\Local\\Temp\\ipykernel_13056\\1786483205.py:2: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  data_new['Return_2'][i]=(data_new['Close'][i]/data_new['Preclose'][i])-1\n",
      "C:\\Users\\18762\\AppData\\Local\\Temp\\ipykernel_13056\\1786483205.py:2: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0!\n",
      "You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy.\n",
      "A typical example is when you are setting values in a column of a DataFrame, like:\n",
      "\n",
      "df[\"col\"][row_indexer] = value\n",
      "\n",
      "Use `df.loc[row_indexer, \"col\"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`.\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "\n",
      "  data_new['Return_2'][i]=(data_new['Close'][i]/data_new['Preclose'][i])-1\n",
      "C:\\Users\\18762\\AppData\\Local\\Temp\\ipykernel_13056\\1786483205.py:2: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  data_new['Return_2'][i]=(data_new['Close'][i]/data_new['Preclose'][i])-1\n",
      "C:\\Users\\18762\\AppData\\Local\\Temp\\ipykernel_13056\\1786483205.py:2: FutureWarning: Series.__setitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To set a value by position, use `ser.iloc[pos] = value`\n",
      "  data_new['Return_2'][i]=(data_new['Close'][i]/data_new['Preclose'][i])-1\n",
      "C:\\Users\\18762\\AppData\\Local\\Temp\\ipykernel_13056\\1786483205.py:2: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value '0.021769706820028656' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.\n",
      "  data_new['Return_2'][i]=(data_new['Close'][i]/data_new['Preclose'][i])-1\n",
      "C:\\Users\\18762\\AppData\\Local\\Temp\\ipykernel_13056\\1786483205.py:2: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  data_new['Return_2'][i]=(data_new['Close'][i]/data_new['Preclose'][i])-1\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,len(data_new)):\n",
    "    data_new['Return_2'][i]=(data_new['Close'][i]/data_new['Preclose'][i])-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Money</th>\n",
       "      <th>Return</th>\n",
       "      <th>Return_2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-05-18</th>\n",
       "      <td>582.89</td>\n",
       "      <td>741.81</td>\n",
       "      <td>770.82</td>\n",
       "      <td>682.01</td>\n",
       "      <td>763.51</td>\n",
       "      <td>1007610100</td>\n",
       "      <td>8.360268e+09</td>\n",
       "      <td>0.309870</td>\n",
       "      <td>0.309870</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-05-19</th>\n",
       "      <td>763.51</td>\n",
       "      <td>768.82</td>\n",
       "      <td>855.81</td>\n",
       "      <td>761.38</td>\n",
       "      <td>855.81</td>\n",
       "      <td>1085526700</td>\n",
       "      <td>9.800152e+09</td>\n",
       "      <td>0.120889</td>\n",
       "      <td>0.120889</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose    Open  Highest  Lowest   Close      Volume  \\\n",
       "Day                                                                 \n",
       "1995-05-18    582.89  741.81   770.82  682.01  763.51  1007610100   \n",
       "1995-05-19    763.51  768.82   855.81  761.38  855.81  1085526700   \n",
       "\n",
       "                   Money    Return  Return_2  \n",
       "Day                                           \n",
       "1995-05-18  8.360268e+09  0.309870  0.309870  \n",
       "1995-05-19  9.800152e+09  0.120889  0.120889  "
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new[data_new['Return']>0.1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Money</th>\n",
       "      <th>Return</th>\n",
       "      <th>Return_2</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-05-23</th>\n",
       "      <td>897.42</td>\n",
       "      <td>791.74</td>\n",
       "      <td>833.69</td>\n",
       "      <td>749.83</td>\n",
       "      <td>750.30</td>\n",
       "      <td>840916100</td>\n",
       "      <td>7.530104e+09</td>\n",
       "      <td>-0.163937</td>\n",
       "      <td>-0.163937</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-12-16</th>\n",
       "      <td>1110.04</td>\n",
       "      <td>1005.01</td>\n",
       "      <td>1005.12</td>\n",
       "      <td>999.63</td>\n",
       "      <td>1000.02</td>\n",
       "      <td>143300300</td>\n",
       "      <td>1.378386e+09</td>\n",
       "      <td>-0.099114</td>\n",
       "      <td>-0.099114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-12-17</th>\n",
       "      <td>1000.02</td>\n",
       "      <td>907.65</td>\n",
       "      <td>943.37</td>\n",
       "      <td>903.85</td>\n",
       "      <td>905.58</td>\n",
       "      <td>601468400</td>\n",
       "      <td>5.295694e+09</td>\n",
       "      <td>-0.094438</td>\n",
       "      <td>-0.094438</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-02-18</th>\n",
       "      <td>982.40</td>\n",
       "      <td>980.48</td>\n",
       "      <td>999.23</td>\n",
       "      <td>893.75</td>\n",
       "      <td>894.85</td>\n",
       "      <td>727384900</td>\n",
       "      <td>6.565445e+09</td>\n",
       "      <td>-0.089118</td>\n",
       "      <td>-0.089118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-05-22</th>\n",
       "      <td>1354.88</td>\n",
       "      <td>1248.97</td>\n",
       "      <td>1300.98</td>\n",
       "      <td>1232.20</td>\n",
       "      <td>1235.22</td>\n",
       "      <td>667319500</td>\n",
       "      <td>8.722466e+09</td>\n",
       "      <td>-0.088318</td>\n",
       "      <td>-0.088318</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-08-17</th>\n",
       "      <td>1168.03</td>\n",
       "      <td>1152.67</td>\n",
       "      <td>1152.67</td>\n",
       "      <td>1068.66</td>\n",
       "      <td>1070.41</td>\n",
       "      <td>536182000</td>\n",
       "      <td>3.656666e+09</td>\n",
       "      <td>-0.083577</td>\n",
       "      <td>-0.083577</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007-02-27</th>\n",
       "      <td>3040.60</td>\n",
       "      <td>3048.83</td>\n",
       "      <td>3049.77</td>\n",
       "      <td>2763.39</td>\n",
       "      <td>2771.79</td>\n",
       "      <td>16137566300</td>\n",
       "      <td>1.290000e+11</td>\n",
       "      <td>-0.088407</td>\n",
       "      <td>-0.088407</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007-06-04</th>\n",
       "      <td>4000.74</td>\n",
       "      <td>3981.82</td>\n",
       "      <td>3987.27</td>\n",
       "      <td>3659.09</td>\n",
       "      <td>3670.40</td>\n",
       "      <td>11485935700</td>\n",
       "      <td>1.460000e+11</td>\n",
       "      <td>-0.082570</td>\n",
       "      <td>-0.082570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-07-27</th>\n",
       "      <td>4070.91</td>\n",
       "      <td>3985.57</td>\n",
       "      <td>4051.16</td>\n",
       "      <td>3720.44</td>\n",
       "      <td>3725.56</td>\n",
       "      <td>55600324700</td>\n",
       "      <td>7.210000e+11</td>\n",
       "      <td>-0.084834</td>\n",
       "      <td>-0.084834</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-08-24</th>\n",
       "      <td>3507.74</td>\n",
       "      <td>3373.48</td>\n",
       "      <td>3388.36</td>\n",
       "      <td>3191.88</td>\n",
       "      <td>3209.91</td>\n",
       "      <td>33467179200</td>\n",
       "      <td>3.590000e+11</td>\n",
       "      <td>-0.084907</td>\n",
       "      <td>-0.084907</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose     Open  Highest   Lowest    Close       Volume  \\\n",
       "Day                                                                     \n",
       "1995-05-23    897.42   791.74   833.69   749.83   750.30    840916100   \n",
       "1996-12-16   1110.04  1005.01  1005.12   999.63  1000.02    143300300   \n",
       "1996-12-17   1000.02   907.65   943.37   903.85   905.58    601468400   \n",
       "1997-02-18    982.40   980.48   999.23   893.75   894.85    727384900   \n",
       "1997-05-22   1354.88  1248.97  1300.98  1232.20  1235.22    667319500   \n",
       "1998-08-17   1168.03  1152.67  1152.67  1068.66  1070.41    536182000   \n",
       "2007-02-27   3040.60  3048.83  3049.77  2763.39  2771.79  16137566300   \n",
       "2007-06-04   4000.74  3981.82  3987.27  3659.09  3670.40  11485935700   \n",
       "2015-07-27   4070.91  3985.57  4051.16  3720.44  3725.56  55600324700   \n",
       "2015-08-24   3507.74  3373.48  3388.36  3191.88  3209.91  33467179200   \n",
       "\n",
       "                   Money    Return  Return_2  \n",
       "Day                                           \n",
       "1995-05-23  7.530104e+09 -0.163937 -0.163937  \n",
       "1996-12-16  1.378386e+09 -0.099114 -0.099114  \n",
       "1996-12-17  5.295694e+09 -0.094438 -0.094438  \n",
       "1997-02-18  6.565445e+09 -0.089118 -0.089118  \n",
       "1997-05-22  8.722466e+09 -0.088318 -0.088318  \n",
       "1998-08-17  3.656666e+09 -0.083577 -0.083577  \n",
       "2007-02-27  1.290000e+11 -0.088407 -0.088407  \n",
       "2007-06-04  1.460000e+11 -0.082570 -0.082570  \n",
       "2015-07-27  7.210000e+11 -0.084834 -0.084834  \n",
       "2015-08-24  3.590000e+11 -0.084907 -0.084907  "
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new[data_new['Return']<-0.08]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Money</th>\n",
       "      <th>Return</th>\n",
       "      <th>Return_2</th>\n",
       "      <th>diff</th>\n",
       "      <th>dufe</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-03</th>\n",
       "      <td>647.8700</td>\n",
       "      <td>637.7200</td>\n",
       "      <td>647.7100</td>\n",
       "      <td>630.5300</td>\n",
       "      <td>639.8800</td>\n",
       "      <td>23451800</td>\n",
       "      <td>1.806930e+08</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>dufe</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-04</th>\n",
       "      <td>639.8800</td>\n",
       "      <td>641.9000</td>\n",
       "      <td>655.5100</td>\n",
       "      <td>638.8600</td>\n",
       "      <td>653.8100</td>\n",
       "      <td>42222000</td>\n",
       "      <td>3.069230e+08</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>dufe</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-05</th>\n",
       "      <td>653.8100</td>\n",
       "      <td>655.3800</td>\n",
       "      <td>657.5200</td>\n",
       "      <td>645.8100</td>\n",
       "      <td>646.8900</td>\n",
       "      <td>43012300</td>\n",
       "      <td>3.015330e+08</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>dufe</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-06</th>\n",
       "      <td>646.8900</td>\n",
       "      <td>642.7500</td>\n",
       "      <td>643.8900</td>\n",
       "      <td>636.3300</td>\n",
       "      <td>640.7600</td>\n",
       "      <td>48748200</td>\n",
       "      <td>3.537580e+08</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>dufe</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-09</th>\n",
       "      <td>640.7600</td>\n",
       "      <td>637.5200</td>\n",
       "      <td>637.5500</td>\n",
       "      <td>625.0400</td>\n",
       "      <td>626.0000</td>\n",
       "      <td>50985100</td>\n",
       "      <td>3.985190e+08</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>dufe</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-25</th>\n",
       "      <td>2901.9518</td>\n",
       "      <td>2891.8918</td>\n",
       "      <td>2897.7674</td>\n",
       "      <td>2872.8497</td>\n",
       "      <td>2886.7416</td>\n",
       "      <td>27463950000</td>\n",
       "      <td>2.732820e+11</td>\n",
       "      <td>-0.005241</td>\n",
       "      <td>-0.005241</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>dufe</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-26</th>\n",
       "      <td>2886.7416</td>\n",
       "      <td>2885.9953</td>\n",
       "      <td>2899.1162</td>\n",
       "      <td>2875.3959</td>\n",
       "      <td>2890.8973</td>\n",
       "      <td>27838753600</td>\n",
       "      <td>2.754430e+11</td>\n",
       "      <td>0.001440</td>\n",
       "      <td>0.001440</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>dufe</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-29</th>\n",
       "      <td>2890.8973</td>\n",
       "      <td>2889.4726</td>\n",
       "      <td>2898.9512</td>\n",
       "      <td>2878.5825</td>\n",
       "      <td>2891.8453</td>\n",
       "      <td>25689972700</td>\n",
       "      <td>2.600950e+11</td>\n",
       "      <td>0.000328</td>\n",
       "      <td>0.000328</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>dufe</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-30</th>\n",
       "      <td>2891.8453</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2885.2152</td>\n",
       "      <td>2865.1493</td>\n",
       "      <td>2879.2996</td>\n",
       "      <td>26247883700</td>\n",
       "      <td>2.694770e+11</td>\n",
       "      <td>-0.004338</td>\n",
       "      <td>-0.004338</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>dufe</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2879.2996</td>\n",
       "      <td>2877.5409</td>\n",
       "      <td>2940.5927</td>\n",
       "      <td>2876.3009</td>\n",
       "      <td>2938.7493</td>\n",
       "      <td>41272341700</td>\n",
       "      <td>4.188720e+11</td>\n",
       "      <td>0.020647</td>\n",
       "      <td>0.020647</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>dufe</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7182 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             Preclose       Open    Highest     Lowest      Close  \\\n",
       "Day                                                                 \n",
       "1995-01-03   647.8700   637.7200   647.7100   630.5300   639.8800   \n",
       "1995-01-04   639.8800   641.9000   655.5100   638.8600   653.8100   \n",
       "1995-01-05   653.8100   655.3800   657.5200   645.8100   646.8900   \n",
       "1995-01-06   646.8900   642.7500   643.8900   636.3300   640.7600   \n",
       "1995-01-09   640.7600   637.5200   637.5500   625.0400   626.0000   \n",
       "...               ...        ...        ...        ...        ...   \n",
       "2024-07-25  2901.9518  2891.8918  2897.7674  2872.8497  2886.7416   \n",
       "2024-07-26  2886.7416  2885.9953  2899.1162  2875.3959  2890.8973   \n",
       "2024-07-29  2890.8973  2889.4726  2898.9512  2878.5825  2891.8453   \n",
       "2024-07-30  2891.8453  2885.2152  2885.2152  2865.1493  2879.2996   \n",
       "2024-07-31  2879.2996  2877.5409  2940.5927  2876.3009  2938.7493   \n",
       "\n",
       "                 Volume         Money    Return  Return_2      diff  dufe  \n",
       "Day                                                                        \n",
       "1995-01-03     23451800  1.806930e+08 -0.012333  0.000000 -0.012333  dufe  \n",
       "1995-01-04     42222000  3.069230e+08  0.021770  0.021770  0.000000  dufe  \n",
       "1995-01-05     43012300  3.015330e+08 -0.010584 -0.010584  0.000000  dufe  \n",
       "1995-01-06     48748200  3.537580e+08 -0.009476 -0.009476  0.000000  dufe  \n",
       "1995-01-09     50985100  3.985190e+08 -0.023035 -0.023035  0.000000  dufe  \n",
       "...                 ...           ...       ...       ...       ...   ...  \n",
       "2024-07-25  27463950000  2.732820e+11 -0.005241 -0.005241  0.000000  dufe  \n",
       "2024-07-26  27838753600  2.754430e+11  0.001440  0.001440  0.000000  dufe  \n",
       "2024-07-29  25689972700  2.600950e+11  0.000328  0.000328  0.000000  dufe  \n",
       "2024-07-30  26247883700  2.694770e+11 -0.004338 -0.004338  0.000000  dufe  \n",
       "2024-07-31  41272341700  4.188720e+11  0.020647  0.020647  0.000000  dufe  \n",
       "\n",
       "[7182 rows x 11 columns]"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new['diff']=data_new['Return']-data_new['Return_2']\n",
    "data_new['dufe']='dufe'\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "两种方法计算的差异： -0.01233272107058514\n"
     ]
    }
   ],
   "source": [
    "print('两种方法计算的差异：',data_new['diff'].sum())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 计算上证综指月度、季度、年度的收益率"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 月度收益率"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$t$表示月份\n",
    "$$\n",
    "R_{t}=\\frac{P_{close,t}-P_{close,t-1}}{P_{close,t-1}}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 算法1\n",
    "1.data_new日度数据里面选择月的最后一天close\n",
    "2.把这个close更改一下变成上个月的最后一天\n",
    "3.计算月收益率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "Month_data=data_new.resample('ME')['Close'].last().to_frame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-31</th>\n",
       "      <td>562.5900</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-02-28</th>\n",
       "      <td>549.2600</td>\n",
       "      <td>562.5900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-03-31</th>\n",
       "      <td>646.9200</td>\n",
       "      <td>549.2600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-04-30</th>\n",
       "      <td>579.9300</td>\n",
       "      <td>646.9200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-05-31</th>\n",
       "      <td>700.5100</td>\n",
       "      <td>579.9300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-03-31</th>\n",
       "      <td>3041.1669</td>\n",
       "      <td>3015.1712</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-04-30</th>\n",
       "      <td>3104.8245</td>\n",
       "      <td>3041.1669</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-05-31</th>\n",
       "      <td>3086.8134</td>\n",
       "      <td>3104.8245</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-30</th>\n",
       "      <td>2967.4028</td>\n",
       "      <td>3086.8134</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2938.7493</td>\n",
       "      <td>2967.4028</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>355 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                Close   Preclose\n",
       "Day                             \n",
       "1995-01-31   562.5900        NaN\n",
       "1995-02-28   549.2600   562.5900\n",
       "1995-03-31   646.9200   549.2600\n",
       "1995-04-30   579.9300   646.9200\n",
       "1995-05-31   700.5100   579.9300\n",
       "...               ...        ...\n",
       "2024-03-31  3041.1669  3015.1712\n",
       "2024-04-30  3104.8245  3041.1669\n",
       "2024-05-31  3086.8134  3104.8245\n",
       "2024-06-30  2967.4028  3086.8134\n",
       "2024-07-31  2938.7493  2967.4028\n",
       "\n",
       "[355 rows x 2 columns]"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Month_data['Preclose']=Month_data['Close'].shift(1)\n",
    "Month_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-31</th>\n",
       "      <td>562.5900</td>\n",
       "      <td>549.2600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-02-28</th>\n",
       "      <td>549.2600</td>\n",
       "      <td>646.9200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-03-31</th>\n",
       "      <td>646.9200</td>\n",
       "      <td>579.9300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-04-30</th>\n",
       "      <td>579.9300</td>\n",
       "      <td>700.5100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-05-31</th>\n",
       "      <td>700.5100</td>\n",
       "      <td>630.5800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-03-31</th>\n",
       "      <td>3041.1669</td>\n",
       "      <td>3104.8245</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-04-30</th>\n",
       "      <td>3104.8245</td>\n",
       "      <td>3086.8134</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-05-31</th>\n",
       "      <td>3086.8134</td>\n",
       "      <td>2967.4028</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-30</th>\n",
       "      <td>2967.4028</td>\n",
       "      <td>2938.7493</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2938.7493</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>355 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                Close   Preclose\n",
       "Day                             \n",
       "1995-01-31   562.5900   549.2600\n",
       "1995-02-28   549.2600   646.9200\n",
       "1995-03-31   646.9200   579.9300\n",
       "1995-04-30   579.9300   700.5100\n",
       "1995-05-31   700.5100   630.5800\n",
       "...               ...        ...\n",
       "2024-03-31  3041.1669  3104.8245\n",
       "2024-04-30  3104.8245  3086.8134\n",
       "2024-05-31  3086.8134  2967.4028\n",
       "2024-06-30  2967.4028  2938.7493\n",
       "2024-07-31  2938.7493        NaN\n",
       "\n",
       "[355 rows x 2 columns]"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Month_data['Preclose']=Month_data['Close'].shift(-1)\n",
    "#shift带代表递延一期\n",
    "Month_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-31</th>\n",
       "      <td>562.5900</td>\n",
       "      <td>549.2600</td>\n",
       "      <td>0.024269</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-02-28</th>\n",
       "      <td>549.2600</td>\n",
       "      <td>646.9200</td>\n",
       "      <td>-0.150961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-03-31</th>\n",
       "      <td>646.9200</td>\n",
       "      <td>579.9300</td>\n",
       "      <td>0.115514</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-04-30</th>\n",
       "      <td>579.9300</td>\n",
       "      <td>700.5100</td>\n",
       "      <td>-0.172132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-05-31</th>\n",
       "      <td>700.5100</td>\n",
       "      <td>630.5800</td>\n",
       "      <td>0.110898</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-03-31</th>\n",
       "      <td>3041.1669</td>\n",
       "      <td>3104.8245</td>\n",
       "      <td>-0.020503</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-04-30</th>\n",
       "      <td>3104.8245</td>\n",
       "      <td>3086.8134</td>\n",
       "      <td>0.005835</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-05-31</th>\n",
       "      <td>3086.8134</td>\n",
       "      <td>2967.4028</td>\n",
       "      <td>0.040241</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-30</th>\n",
       "      <td>2967.4028</td>\n",
       "      <td>2938.7493</td>\n",
       "      <td>0.009750</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>2938.7493</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>355 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                Close   Preclose    Return\n",
       "Day                                       \n",
       "1995-01-31   562.5900   549.2600  0.024269\n",
       "1995-02-28   549.2600   646.9200 -0.150961\n",
       "1995-03-31   646.9200   579.9300  0.115514\n",
       "1995-04-30   579.9300   700.5100 -0.172132\n",
       "1995-05-31   700.5100   630.5800  0.110898\n",
       "...               ...        ...       ...\n",
       "2024-03-31  3041.1669  3104.8245 -0.020503\n",
       "2024-04-30  3104.8245  3086.8134  0.005835\n",
       "2024-05-31  3086.8134  2967.4028  0.040241\n",
       "2024-06-30  2967.4028  2938.7493  0.009750\n",
       "2024-07-31  2938.7493        NaN       NaN\n",
       "\n",
       "[355 rows x 3 columns]"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Month_data['Return']=(Month_data['Close']/Month_data['Preclose'])-1\n",
    "Month_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1999-05-31</th>\n",
       "      <td>1279.33</td>\n",
       "      <td>1689.43</td>\n",
       "      <td>-0.242745</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              Close  Preclose    Return\n",
       "Day                                    \n",
       "1999-05-31  1279.33   1689.43 -0.242745"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Month_data[Month_data['Return']==Month_data['Return'].min()]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 算法2\n",
    "$$\n",
    "R_t=\\prod(R_{t,d}+1)-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Return_plus1</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-31</th>\n",
       "      <td>0.868369</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-02-28</th>\n",
       "      <td>0.976306</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-03-31</th>\n",
       "      <td>1.177803</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-04-30</th>\n",
       "      <td>0.896448</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-05-31</th>\n",
       "      <td>1.207922</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-03-31</th>\n",
       "      <td>1.008622</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-04-30</th>\n",
       "      <td>1.020932</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-05-31</th>\n",
       "      <td>0.994199</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-30</th>\n",
       "      <td>0.961316</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>0.990344</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>355 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Return_plus1\n",
       "Day                     \n",
       "1995-01-31      0.868369\n",
       "1995-02-28      0.976306\n",
       "1995-03-31      1.177803\n",
       "1995-04-30      0.896448\n",
       "1995-05-31      1.207922\n",
       "...                  ...\n",
       "2024-03-31      1.008622\n",
       "2024-04-30      1.020932\n",
       "2024-05-31      0.994199\n",
       "2024-06-30      0.961316\n",
       "2024-07-31      0.990344\n",
       "\n",
       "[355 rows x 1 columns]"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new['Return_plus1']=data_new['Return']+1\n",
    "Month_data2=data_new.resample('ME')['Return_plus1'].prod().to_frame()\n",
    "Month_data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Return_plus1</th>\n",
       "      <th>Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-31</th>\n",
       "      <td>0.868369</td>\n",
       "      <td>-0.131631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-02-28</th>\n",
       "      <td>0.976306</td>\n",
       "      <td>-0.023694</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-03-31</th>\n",
       "      <td>1.177803</td>\n",
       "      <td>0.177803</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-04-30</th>\n",
       "      <td>0.896448</td>\n",
       "      <td>-0.103552</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-05-31</th>\n",
       "      <td>1.207922</td>\n",
       "      <td>0.207922</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-03-31</th>\n",
       "      <td>1.008622</td>\n",
       "      <td>0.008622</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-04-30</th>\n",
       "      <td>1.020932</td>\n",
       "      <td>0.020932</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-05-31</th>\n",
       "      <td>0.994199</td>\n",
       "      <td>-0.005801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-06-30</th>\n",
       "      <td>0.961316</td>\n",
       "      <td>-0.038684</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-07-31</th>\n",
       "      <td>0.990344</td>\n",
       "      <td>-0.009656</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>355 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Return_plus1    Return\n",
       "Day                               \n",
       "1995-01-31      0.868369 -0.131631\n",
       "1995-02-28      0.976306 -0.023694\n",
       "1995-03-31      1.177803  0.177803\n",
       "1995-04-30      0.896448 -0.103552\n",
       "1995-05-31      1.207922  0.207922\n",
       "...                  ...       ...\n",
       "2024-03-31      1.008622  0.008622\n",
       "2024-04-30      1.020932  0.020932\n",
       "2024-05-31      0.994199 -0.005801\n",
       "2024-06-30      0.961316 -0.038684\n",
       "2024-07-31      0.990344 -0.009656\n",
       "\n",
       "[355 rows x 2 columns]"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new['Return_plus1']=data_new['Return']+1\n",
    "Month_data2=data_new.resample('ME')['Return_plus1'].prod().to_frame()\n",
    "Month_data2['Return']=Month_data2['Return_plus1']-1\n",
    "Month_data2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 年度收益率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-12-31</th>\n",
       "      <td>555.2900</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-12-31</th>\n",
       "      <td>917.0200</td>\n",
       "      <td>555.2900</td>\n",
       "      <td>0.651425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-12-31</th>\n",
       "      <td>1194.1000</td>\n",
       "      <td>917.0200</td>\n",
       "      <td>0.302153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-12-31</th>\n",
       "      <td>1146.7000</td>\n",
       "      <td>1194.1000</td>\n",
       "      <td>-0.039695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1999-12-31</th>\n",
       "      <td>1366.5800</td>\n",
       "      <td>1146.7000</td>\n",
       "      <td>0.191750</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-12-31</th>\n",
       "      <td>2073.4800</td>\n",
       "      <td>1366.5800</td>\n",
       "      <td>0.517277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-12-31</th>\n",
       "      <td>1645.9700</td>\n",
       "      <td>2073.4800</td>\n",
       "      <td>-0.206180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-12-31</th>\n",
       "      <td>1357.6500</td>\n",
       "      <td>1645.9700</td>\n",
       "      <td>-0.175167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2003-12-31</th>\n",
       "      <td>1497.0400</td>\n",
       "      <td>1357.6500</td>\n",
       "      <td>0.102670</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-12-31</th>\n",
       "      <td>1266.5000</td>\n",
       "      <td>1497.0400</td>\n",
       "      <td>-0.153997</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005-12-31</th>\n",
       "      <td>1161.0600</td>\n",
       "      <td>1266.5000</td>\n",
       "      <td>-0.083253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006-12-31</th>\n",
       "      <td>2675.4700</td>\n",
       "      <td>1161.0600</td>\n",
       "      <td>1.304334</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007-12-31</th>\n",
       "      <td>5261.5600</td>\n",
       "      <td>2675.4700</td>\n",
       "      <td>0.966593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-12-31</th>\n",
       "      <td>1820.8100</td>\n",
       "      <td>5261.5600</td>\n",
       "      <td>-0.653941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009-12-31</th>\n",
       "      <td>3277.1400</td>\n",
       "      <td>1820.8100</td>\n",
       "      <td>0.799825</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010-12-31</th>\n",
       "      <td>2808.0800</td>\n",
       "      <td>3277.1400</td>\n",
       "      <td>-0.143131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-12-31</th>\n",
       "      <td>2199.4200</td>\n",
       "      <td>2808.0800</td>\n",
       "      <td>-0.216753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-12-31</th>\n",
       "      <td>2269.1300</td>\n",
       "      <td>2199.4200</td>\n",
       "      <td>0.031695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-12-31</th>\n",
       "      <td>2115.9800</td>\n",
       "      <td>2269.1300</td>\n",
       "      <td>-0.067493</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-12-31</th>\n",
       "      <td>3234.6800</td>\n",
       "      <td>2115.9800</td>\n",
       "      <td>0.528691</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-31</th>\n",
       "      <td>3539.1800</td>\n",
       "      <td>3234.6800</td>\n",
       "      <td>0.094136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-12-31</th>\n",
       "      <td>3103.6400</td>\n",
       "      <td>3539.1800</td>\n",
       "      <td>-0.123062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-31</th>\n",
       "      <td>3307.1700</td>\n",
       "      <td>3103.6400</td>\n",
       "      <td>0.065578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-31</th>\n",
       "      <td>2493.9000</td>\n",
       "      <td>3307.1700</td>\n",
       "      <td>-0.245911</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-31</th>\n",
       "      <td>3050.1200</td>\n",
       "      <td>2493.9000</td>\n",
       "      <td>0.223032</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-12-31</th>\n",
       "      <td>3473.0700</td>\n",
       "      <td>3050.1200</td>\n",
       "      <td>0.138667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-12-31</th>\n",
       "      <td>3639.7800</td>\n",
       "      <td>3473.0700</td>\n",
       "      <td>0.048001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-31</th>\n",
       "      <td>3089.2579</td>\n",
       "      <td>3639.7800</td>\n",
       "      <td>-0.151251</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-31</th>\n",
       "      <td>2974.9348</td>\n",
       "      <td>3089.2579</td>\n",
       "      <td>-0.037007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-31</th>\n",
       "      <td>2938.7493</td>\n",
       "      <td>2974.9348</td>\n",
       "      <td>-0.012163</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                Close   Preclose    Return\n",
       "Day                                       \n",
       "1995-12-31   555.2900        NaN       NaN\n",
       "1996-12-31   917.0200   555.2900  0.651425\n",
       "1997-12-31  1194.1000   917.0200  0.302153\n",
       "1998-12-31  1146.7000  1194.1000 -0.039695\n",
       "1999-12-31  1366.5800  1146.7000  0.191750\n",
       "2000-12-31  2073.4800  1366.5800  0.517277\n",
       "2001-12-31  1645.9700  2073.4800 -0.206180\n",
       "2002-12-31  1357.6500  1645.9700 -0.175167\n",
       "2003-12-31  1497.0400  1357.6500  0.102670\n",
       "2004-12-31  1266.5000  1497.0400 -0.153997\n",
       "2005-12-31  1161.0600  1266.5000 -0.083253\n",
       "2006-12-31  2675.4700  1161.0600  1.304334\n",
       "2007-12-31  5261.5600  2675.4700  0.966593\n",
       "2008-12-31  1820.8100  5261.5600 -0.653941\n",
       "2009-12-31  3277.1400  1820.8100  0.799825\n",
       "2010-12-31  2808.0800  3277.1400 -0.143131\n",
       "2011-12-31  2199.4200  2808.0800 -0.216753\n",
       "2012-12-31  2269.1300  2199.4200  0.031695\n",
       "2013-12-31  2115.9800  2269.1300 -0.067493\n",
       "2014-12-31  3234.6800  2115.9800  0.528691\n",
       "2015-12-31  3539.1800  3234.6800  0.094136\n",
       "2016-12-31  3103.6400  3539.1800 -0.123062\n",
       "2017-12-31  3307.1700  3103.6400  0.065578\n",
       "2018-12-31  2493.9000  3307.1700 -0.245911\n",
       "2019-12-31  3050.1200  2493.9000  0.223032\n",
       "2020-12-31  3473.0700  3050.1200  0.138667\n",
       "2021-12-31  3639.7800  3473.0700  0.048001\n",
       "2022-12-31  3089.2579  3639.7800 -0.151251\n",
       "2023-12-31  2974.9348  3089.2579 -0.037007\n",
       "2024-12-31  2938.7493  2974.9348 -0.012163"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Year_data=data_new.resample('YE')['Close'].last().to_frame()\n",
    "Year_data['Preclose']=Year_data['Close'].shift(1)\n",
    "Year_data['Return']=(Year_data['Close']/Year_data['Preclose'])-1\n",
    "Year_data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.12609727342069055)"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mean_return=Year_data['Return'].mean()\n",
    "mean_return"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
